﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Agnes.Core
{
    /// <summary>
    /// SaveDialog.xaml 的交互逻辑
    /// </summary>
    public partial class SaveDialog : Window
    {
        /// <summary>
        /// 保存结果
        /// </summary>
        public SaveResult SaveResult { get; private set; }

        /// <summary>
        /// 是否仅限于当前当前激活的文档
        /// </summary>
        public bool JustActiveDocument { get; set; }

        public SaveDialog()
        {
            InitializeComponent();

            SaveResult = SaveResult.Cancel;
            DataContext = this;
        }

        public void ShowSDialog()
        {
            if (!JustActiveDocument)
            {
                foreach (IDocumentPanel i in AgnesServiceManager.AgnesWorkBenchService.DocumentPanels)
                {
                    if (i.IsDirty)
                    {
                        string tag = i.Caption;
                        if (!i.IsVaild)
                        {
                            tag += "[有错误！]";
                            btnSave.IsEnabled = false;
                        }
                        lbTags.Items.Add(tag);
                    }

                }
            }
            else
            {
                IDocumentPanel documentPanel = AgnesServiceManager.AgnesWorkBenchService.FocusedDocumentPanel;
                if (documentPanel != null)
                {
                    if (documentPanel.IsDirty)
                    {
                        string tag = documentPanel.Caption;
                        if (!documentPanel.IsVaild)
                        {
                            tag += "[有错误！]";
                            btnSave.IsEnabled = false;
                        }
                        lbTags.Items.Add(tag);
                    }
                }
            }

            if (lbTags.Items.Count != 0)
            {
                this.ShowDialog();
            }
            else
            {
                this.SaveResult = SaveResult.Save;
            }
        }

        private void DoNotSave(object sender, RoutedEventArgs e)
        {
            SaveResult = SaveResult.DoNotSave;
            this.Close();
        }

        private void Cancel(object sender, RoutedEventArgs e)
        {
            SaveResult = SaveResult.Cancel;
            this.Close();
        }

        private void Save(object sender, RoutedEventArgs e)
        {
            SaveResult = SaveResult.Save;
            if (!JustActiveDocument)
            {
                foreach (IDocumentPanel i in AgnesServiceManager.AgnesWorkBenchService.DocumentPanels)
                {
                    i.Save();
                }
            }
            else
            {
                IDocumentPanel documentPanel = AgnesServiceManager.AgnesWorkBenchService.FocusedDocumentPanel;
                if (documentPanel != null)
                {
                    documentPanel.Save();
                }
            }
            this.Close();
        }
    }

    public enum SaveResult
    {
        Save,
        DoNotSave,
        Cancel
    }
}
